<?php

namespace app\admin\controller\meet;

use app\common\controller\Backend;
use app\common\model\meet\Config as ConfigModel;
use think\Exception;

/**
 * 配置管理
 *
 * @icon fa fa-circle-o
 */
class Config extends Backend
{

    /**
     * Config模型对象
     * @var ConfigModel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ConfigModel;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     */
    public function index()
    {
        return $this->view->fetch();
    }

    public function platform($group = null)
    {
        //$list = $this->model->where('group','=',$group)->column('*','name');
        $list = $this->model->where('group','in',$group)->select();
        $config_list = [];
        if($list){
            foreach ($list as $key => $value){
                $value = $value->toArray();
                $value['title'] = __($value['title']);
                if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                    $value['value'] = explode(',', $value['value']);
                }
                $value['content'] = json_decode($value['content'], true);
                if (in_array($value['name'], ['categorytype', 'configgroup', 'attachmentcategory'])) {
                    $dictValue = (array)json_decode($value['value'], true);
                    foreach ($dictValue as $index => &$item) {
                        $item = __($item);
                    }
                    unset($item);
                    $value['value'] = json_encode($dictValue, JSON_UNESCAPED_UNICODE);
                }
                $value['tip'] = htmlspecialchars($value['tip']);

                $config_list[$value['name']] = $value;
            }
        }

        if (false === $this->request->isPost()) {
            $registerCoupon = [];
            $shareCoupon = [];
            if($group == 'user'){
                $registerCoupon = $config_list['register_send']['value'] ? json_decode($config_list['register_send']['value'], true) : [];
                $shareCoupon = $config_list['share_coupon_send']['value'] ? json_decode($config_list['share_coupon_send']['value'], true) : [];

                $template_model = new \app\admin\model\meet\CouponTemplate();

                foreach ($registerCoupon as $key => $value){
                    $template = $template_model->where('id', '=', $value['id'])->find();

                    if (empty($template)) {
                        unset($registerCoupon[$key]);
                        continue;
                    }

                    $registerCoupon[$key] = $template;
                    $registerCoupon[$key]['send_number'] = $value['send_number'];
                }

                foreach ($shareCoupon as $key => $value){
                    $template = $template_model->where('id', '=', $value['id'])->find();

                    if (empty($template)) {
                        unset($shareCoupon[$key]);
                        continue;
                    }

                    $shareCoupon[$key] = $template;
                    $shareCoupon[$key]['send_number'] = $value['send_number'];
                }
            }

            $config_list['register_send']['value'] = json_encode($registerCoupon);
            $config_list['share_coupon_send']['value'] = json_encode($shareCoupon);

            $this->view->assign('group',$group);
            $this->view->assign('configList',$config_list);
            return $this->view->fetch();
        }

        $this->token();
        $row = $this->request->post("row/a", [], 'trim');
        if ($row) {
            $configList = [];

            if(!isset($row['register_send']) && $group == 'user'){
                $row['register_send'] = '';
            }

            if(!isset($row['share_coupon_send']) && $group == 'user'){
                $row['share_coupon_send'] = '';
            }
            foreach ($this->model->all() as $v) {
                if (isset($row[$v['name']])) {
                    $value = $row[$v['name']];

                    if($v['name'] == 'register_send' || $v['name'] == 'share_coupon_send'){
                        $v['value'] = $value ? json_encode($value) : '';
                    }else{
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }

                        $v['value'] = $value;
                    }

                    $configList[] = $v->toArray();
                }
            }
            try {
                $this->model->allowField(true)->saveAll($configList);
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
            try {
                ConfigModel::refreshCache();
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', ''));
    }
}
